package com.ybl.store.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ybl.store.backend.dao.entity.AdminDO;
import com.ybl.store.backend.dao.entity.ClerkDO;
import com.ybl.store.backend.dao.entity.ManagerDO;
import com.ybl.store.backend.dao.mapper.AdminMapper;
import com.ybl.store.backend.dao.mapper.ClerkMapper;
import com.ybl.store.backend.dao.mapper.ManagerMapper;
import com.ybl.store.backend.dto.UserInfoDTO;
import com.ybl.store.backend.dto.req.LoginReqDTO;
import com.ybl.store.backend.dto.resp.LoginRespDTO;
import com.ybl.store.backend.service.LoginService;
import com.ybl.store.common.constant.UserIdentity;
import com.ybl.store.common.convention.errorcode.BaseErrorCode;
import com.ybl.store.common.convention.exception.ClientException;
import com.ybl.store.common.properties.JwtProperties;
import com.ybl.store.common.util.JwtUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 管理员登录服务层
 */
@Service
@RequiredArgsConstructor
public class LoginServiceImpl implements LoginService {

    private final AdminMapper adminMapper;

    private final ManagerMapper managerMapper;

    private final ClerkMapper clerkMapper;

    private final JwtProperties jwtProperties;

    @Override
    public LoginRespDTO login(LoginReqDTO requestParam) {
        // 判断登录人是否是管理员
        LambdaQueryWrapper<AdminDO> adminDOLambdaQueryWrapper = Wrappers.lambdaQuery(AdminDO.class)
                .eq(AdminDO::getUsername, requestParam.getUsername())
                .eq(AdminDO::getPassword, requestParam.getPassword());
        AdminDO adminDO = adminMapper.selectOne(adminDOLambdaQueryWrapper);

        if (adminDO != null) {
            UserInfoDTO userInfoDTO = UserInfoDTO.builder()
                    .id(adminDO.getId())
                    .username(adminDO.getUsername())
                    .identity(UserIdentity.admin)
                    .build();
            String token = JwtUtil.createJwtToken(jwtProperties.getSecretKey(), userInfoDTO);

            return new LoginRespDTO(token, userInfoDTO.getIdentity());
        }

        // 判断登录人是否是店长
        LambdaQueryWrapper<ManagerDO> managerDOLambdaQueryWrapper = Wrappers.lambdaQuery(ManagerDO.class)
                .eq(ManagerDO::getUsername, requestParam.getUsername())
                .eq(ManagerDO::getPassword, requestParam.getPassword());
        ManagerDO managerDO = managerMapper.selectOne(managerDOLambdaQueryWrapper);

        if (managerDO != null) {
            UserInfoDTO userInfoDTO = UserInfoDTO.builder()
                    .id(managerDO.getId())
                    .username(managerDO.getUsername())
                    .identity(UserIdentity.manager)
                    .build();
            String token = JwtUtil.createJwtToken(jwtProperties.getSecretKey(), userInfoDTO);

            return new LoginRespDTO(token, userInfoDTO.getIdentity());
        }

        // 判断登陆人是否是店员
        LambdaQueryWrapper<ClerkDO> clerkDOLambdaQueryWrapper = Wrappers.lambdaQuery(ClerkDO.class)
                .eq(ClerkDO::getUsername, requestParam.getUsername())
                .eq(ClerkDO::getPassword, requestParam.getPassword());
        ClerkDO clerkDO = clerkMapper.selectOne(clerkDOLambdaQueryWrapper);

        if (clerkDO != null) {
            UserInfoDTO userInfoDTO = UserInfoDTO.builder()
                    .id(clerkDO.getId())
                    .username(clerkDO.getUsername())
                    .identity(UserIdentity.clerk)
                    .build();
            String token = JwtUtil.createJwtToken(jwtProperties.getSecretKey(), userInfoDTO);

            return new LoginRespDTO(token, userInfoDTO.getIdentity());
        }

        throw new ClientException(BaseErrorCode.USER_LOGIN_ERROR);
    }
}
